﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ThongTinGiangVien : System.Web.UI.Page
{
    HocVien hv = new HocVien();
    int action;
    string msgv = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PageLoad();
        }
    }

    private void PageLoad()
    {
        action = int.Parse(Request.QueryString["action"]);
        if (action == 1)
        { 
        }
        if (action == 2)
        {
            msgv = Request.QueryString["MSGV"].ToString();
            if (msgv != "")
                LoadThongTin(Request.QueryString["MSGV"].ToString());
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!!Không có mã số giảng viên');", true);
                Response.Redirect("./QuanLyGiangVien.aspx");
            }
        }
    }

    private void LoadThongTin(string p)
    {
        DataClassesDataContext data = new DataClassesDataContext();
        var gvs = from temp in data.tbl_GIANGVIENs
                 where temp.MsGiangVien == p
                 select temp;
        foreach (tbl_GIANGVIEN gv in gvs)
        {
            txtHoTenGV.Text = gv.TenGiangVien;
            txtNgaySinh.Text = gv.NgaySinh.Value.ToShortDateString();
            txtDiaChi.Text = gv.DiaChi;
            txtEmail.Text =gv.Email;
            txtSoDT.Text = gv.SoDT;
            ddlGioiTinh.SelectedValue = gv.GioiTinh.Value.ToString();
        }
        txtMSGV.Text = p;
        txtMSGV.Enabled = false;
   }

    private string TuDongSinhMaHocVien()
    {
        string MaHV = "GV001";
        int a = 1;
        while (hv.KiemTraMaHV(MaHV) == true)
        {
            if (a < 9)
            {
                a++;
                MaHV = "GV00" + a;
            }
            else if (a >= 9 && a < 99)
            {
                a++;
                MaHV = "GV0" + a;
            }
            else if (a >= 99)
            {
                a++;
                MaHV = "GV" + a;
            }
        }
        return MaHV;
    }

    protected void btnSavePerson_Click(object sender, EventArgs e)
    {
        try
        {
            action = int.Parse(Request.QueryString["action"]);

            if (action == 1)
            {
                if (txtNgaySinh.Text == "") return;
                DataClassesDataContext data = new DataClassesDataContext();

                tbl_GIANGVIEN gv = new tbl_GIANGVIEN();
                gv.MsGiangVien = txtMSGV.Text.ToString();
                gv.TenGiangVien = txtHoTenGV.Text.ToString();
                gv.GioiTinh = bool.Parse(ddlGioiTinh.SelectedValue.ToString());
                gv.NgaySinh = DateTime.Parse(txtNgaySinh.Text.ToString());
                gv.DiaChi = txtDiaChi.Text.ToString();
                gv.Email = txtEmail.Text.ToString();
                gv.SoDT = txtSoDT.Text.ToString();

                data.tbl_GIANGVIENs.InsertOnSubmit(gv);
                data.SubmitChanges();
                
                tbl_DANGNHAP tk = new tbl_DANGNHAP();
                tk.TenDangNhap = txtMSGV.Text;
                tk.MatKhau = txtMSGV.Text;
                tk.MaQuyen = "nhanvien";

                data.tbl_DANGNHAPs.InsertOnSubmit(tk);
                data.SubmitChanges();
            }
            if (action == 2)
            {
                msgv = Request.QueryString["MSGV"].ToString();

                DataClassesDataContext data = new DataClassesDataContext();

                var SuaGV = from p in data.tbl_GIANGVIENs
                             where p.MsGiangVien == msgv
                             select p;


                foreach (tbl_GIANGVIEN Sua_SV in SuaGV)
                {
                    Sua_SV.TenGiangVien = txtHoTenGV.Text.ToString();
                    Sua_SV.GioiTinh = bool.Parse(ddlGioiTinh.SelectedValue.ToString());
                    Sua_SV.NgaySinh = DateTime.Parse(txtNgaySinh.Text.ToString());
                    Sua_SV.DiaChi = txtDiaChi.Text.ToString();
                    Sua_SV.Email = txtEmail.Text.ToString();
                    Sua_SV.SoDT = txtSoDT.Text.ToString();
                }

                data.SubmitChanges(); 
            }
            Response.Redirect("./QuanLyGiangVien.aspx");

        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!!"+ex.Message.ToString()+"');", true);
        }

    }
}
